/**
  ******************************************************************************
  * @file    lwip.c
  * @author  Iron
  * @date    2018-12-02
  * @version v1.0
  * @brief   lwip c file
  */

/** @addtogroup GROUP_MAIN
  * @{
  */

/* includes ------------------------------------------------------------------*/
#include "bsp_eth.h"
#include "netif/ethernet.h"
#include "lwip/tcpip.h"
#if defined ( __CC_ARM )  /* MDK ARM Compiler */
    #include "lwip/sio.h"
#endif /* MDK ARM Compiler */

/* private typedef -----------------------------------------------------------*/
/* private macro -------------------------------------------------------------*/
/* private variables ---------------------------------------------------------*/
/* private function prototypes -----------------------------------------------*/
/* private functions ---------------------------------------------------------*/

/**
  * LwIP initialization function
  */
void MX_LWIP_Init(void)
{
    /* Initilialize the LwIP stack without RTOS */
//  lwip_init();

    /* Create tcp_ip stack thread */
    tcpip_init(NULL, NULL);

    bsp_eth_init();
}


#ifdef USE_OBSOLETE_USER_CODE_SECTION_4
    /* Kept to help code migration. (See new 4_1, 4_2... sections) */
    /* Avoid to use this user section which will become obsolete. */
    /* USER CODE BEGIN 4 */
    /* USER CODE END 4 */
#endif

#if defined ( __CC_ARM )  /* MDK ARM Compiler */
/**
 * Opens a serial device for communication.
 *
 * @param devnum device number
 * @return handle to serial device if successful, NULL otherwise
 */
sio_fd_t sio_open(u8_t devnum)
{
    sio_fd_t sd;

    /* USER CODE BEGIN 7 */
    sd = 0; // dummy code
    /* USER CODE END 7 */

    return sd;
}

/**
 * Sends a single character to the serial device.
 *
 * @param c character to send
 * @param fd serial device handle
 *
 * @note This function will block until the character can be sent.
 */
void sio_send(u8_t c, sio_fd_t fd)
{
    /* USER CODE BEGIN 8 */
    /* USER CODE END 8 */
}

/**
 * Reads from the serial device.
 *
 * @param fd serial device handle
 * @param data pointer to data buffer for receiving
 * @param len maximum length (in bytes) of data to receive
 * @return number of bytes actually received - may be 0 if aborted by sio_read_abort
 *
 * @note This function will block until data can be received. The blocking
 * can be cancelled by calling sio_read_abort().
 */
u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
{
    u32_t recved_bytes;

    /* USER CODE BEGIN 9 */
    recved_bytes = 0; // dummy code
    /* USER CODE END 9 */
    return recved_bytes;
}

/**
 * Tries to read from the serial device. Same as sio_read but returns
 * immediately if no data is available and never blocks.
 *
 * @param fd serial device handle
 * @param data pointer to data buffer for receiving
 * @param len maximum length (in bytes) of data to receive
 * @return number of bytes actually received
 */
u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
{
    u32_t recved_bytes;

    /* USER CODE BEGIN 10 */
    recved_bytes = 0; // dummy code
    /* USER CODE END 10 */
    return recved_bytes;
}
#endif /* MDK ARM Compiler */

/**
 * ----------------------------------------------------------------------
 * Function given to help user to continue LwIP Initialization
 * Up to user to complete or change this function ...
 * Up to user to call this function in main.c in while (1) of main(void)
 *-----------------------------------------------------------------------
 * Read a received packet from the Ethernet buffers
 * Send it to the lwIP stack for handling
 * Handle timeouts if LWIP_TIMERS is set and without RTOS
 * Handle the llink status if LWIP_NETIF_LINK_CALLBACK is set and without RTOS
 */
void MX_LWIP_Process(void)
{
    /* USER CODE BEGIN 4_1 */
    /* USER CODE END 4_1 */
    

    /* USER CODE BEGIN 4_2 */
    /* USER CODE END 4_2 */
    /* Handle timeouts */
//  sys_check_timeouts();

    /* USER CODE BEGIN 4_3 */
    /* USER CODE END 4_3 */
}

#if defined ( __CC_ARM )  /* MDK ARM Compiler */
/**
 * Opens a serial device for communication.
 *
 * @param devnum device number
 * @return handle to serial device if successful, NULL otherwise
 */
sio_fd_t sio_open(u8_t devnum)
{
    sio_fd_t sd;

    /* USER CODE BEGIN 7 */
    sd = 0; // dummy code
    /* USER CODE END 7 */

    return sd;
}

/**
 * Sends a single character to the serial device.
 *
 * @param c character to send
 * @param fd serial device handle
 *
 * @note This function will block until the character can be sent.
 */
void sio_send(u8_t c, sio_fd_t fd)
{
    /* USER CODE BEGIN 8 */
    /* USER CODE END 8 */
}

/**
 * Reads from the serial device.
 *
 * @param fd serial device handle
 * @param data pointer to data buffer for receiving
 * @param len maximum length (in bytes) of data to receive
 * @return number of bytes actually received - may be 0 if aborted by sio_read_abort
 *
 * @note This function will block until data can be received. The blocking
 * can be cancelled by calling sio_read_abort().
 */
u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
{
    u32_t recved_bytes;

    /* USER CODE BEGIN 9 */
    recved_bytes = 0; // dummy code
    /* USER CODE END 9 */
    return recved_bytes;
}

/**
 * Tries to read from the serial device. Same as sio_read but returns
 * immediately if no data is available and never blocks.
 *
 * @param fd serial device handle
 * @param data pointer to data buffer for receiving
 * @param len maximum length (in bytes) of data to receive
 * @return number of bytes actually received
 */
u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
{
    u32_t recved_bytes;

    /* USER CODE BEGIN 10 */
    recved_bytes = 0; // dummy code
    /* USER CODE END 10 */
    return recved_bytes;
}
#endif /* MDK ARM Compiler */

/**
  * @}
  */

/******************* (C)COPYRIGHT 2018 ***** END OF FILE *********************/
